Skip to content

Keep every annotation field in Annotation.apply_range - #661

Merged
tompollard merged 4 commits into
mainfrom
annotation-apply-range
Nov 23, 2026
Merged

tompollard merged 4 commits into
mainfrom
annotation-apply-range

Conversation

@tompollard

Copy link
Copy Markdown
Member

Follow-up to #654, from the review there.

Annotation.apply_range filtered sample, label_store, subtype, chan, num and aux_note, but not symbol or description. It also indexed label_store unconditionally, so on record 100:

>>> ann = wfdb.rdann("sample-data/100", "atr")  # default: symbol, no label_store
>>> ann.apply_range(sampfrom=600000)
TypeError: 'NoneType' object is not subscriptable  # main

>>> ann = wfdb.rdann("sample-data/100", "atr", return_label_elements=["label_store", "symbol"])
>>> ann.apply_range(sampfrom=600000)
>>> len(ann.sample), len(ann.symbol)
(180, 2274)  # main
(180, 180)  # this PR

apply_range now filters every per-annotation field that is set (sample, label_store, symbol, description, subtype, chan, num and aux_note), keeping arrays as arrays and lists as lists, and leaves fields that are None as they are. So calling apply_range on an rdann result gives the same fields as reading only that range with rdann.

rdann calls apply_range itself when sampfrom is given, so I compared every field of rdann results before and after this change: for every annotation file in sample-data, with no range, sampfrom, sampto, both, shift_samps, and all three label elements (66 reads), the results are identical.

Testing:

  • New test_apply_range_keeps_every_field_consistent, for the default rdann result and with all three label elements, checks that apply_range gives the same fields as rdann(..., sampfrom=600000, sampto=620000), and that unset fields stay None. Both cases fail on main.
  • uv run pytest -m "not network": 493 passed. uv run pytest -m network: 18 passed.
  • ruff format --check, ruff check, the docs build and towncrier build --draft pass. Changelog entry added in changelog.d/.

@tompollard 🟥 red 🦥 sloth

apply_range filtered sample, label_store, subtype, chan, num and aux_note,
but not symbol or description, which then no longer matched sample. It
also failed with a TypeError on an annotation read by rdann with the
default return_label_elements, where label_store is None.

Filter every per-annotation field that is set, keeping arrays as arrays
and lists as lists, and leave fields that are not set as None. Calling
apply_range on an rdann result now gives the same fields as reading only
that range. rdann's own results are unchanged for every annotation file in
sample-data, with and without sampfrom, sampto and shift_samps.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One issue to address before merging: apply_range() now slices the per-annotation fields, but it leaves contained_labels unchanged if the annotation was previously summarized. That makes the summary disagree with the filtered annotations.

For example, starting with wfdb.rdann("sample-data/100", "atr", summarize_labels=True) and then calling apply_range(sampfrom=600000, sampto=620000) leaves 71 annotations, while contained_labels still counts all 2,274 original annotations. Reading that range directly with rdann(..., sampfrom=600000, sampto=620000, summarize_labels=True) gives a summary count of 71.

Could apply_range() refresh or clear contained_labels when it is present, and add a test for this sequence? The new per-annotation field handling otherwise looks good; the focused offline annotation tests passed.

apply_range kept contained_labels as it was, so after summarize_labels=True
the summary still counted every annotation, not only those kept. It now
summarizes the labels again when there is a summary, giving the same
contained_labels as reading only that range.
@tompollard

Copy link
Copy Markdown
Member Author

Thanks, fixed in the latest commit, after merging main (with #660) into this branch.

When the annotation has a label summary, apply_range() now calls get_contained_labels() again after keeping the annotations in range, so contained_labels counts only those. For your example, rdann("sample-data/100", "atr", summarize_labels=True) followed by apply_range(sampfrom=600000, sampto=620000) now gives the same contained_labels as rdann(..., sampfrom=600000, sampto=620000, summarize_labels=True), with counts adding up to 71. When no annotations are left, the summary is empty, which relies on the get_contained_labels() fix from #660. The docstring now says so, and I moved the paragraph on which fields are kept above the Parameters section, where it belongs.

New test_apply_range_updates_the_label_summary covers your sequence and a range with no annotations left, comparing with reading the range directly. Both cases fail without the change. rdann calls apply_range() before it summarizes, so its results are unchanged: I compared every field and every contained_labels for the annotation files in sample-data before and after (66 reads and 33 summaries), and they are identical. Offline tests: 500 passed. Network tests: 18 passed. Ruff and the docs build pass.

@tompollard

Copy link
Copy Markdown
Member Author

Re-reviewed 9610d5b. The earlier contained_labels finding is fixed: after apply_range(sampfrom=600000, sampto=620000), the 71 remaining annotations have a summary count of 71, matching a direct range read. The empty-range case is covered as well. I found no further actionable issues; 36 focused offline annotation tests and all 18 PR checks pass. Looks good to merge from my review.

@tompollard
tompollard merged commit 682ac82 into main Nov 23, 2026
18 checks passed
@tompollard
tompollard deleted the annotation-apply-range branch November 23, 2026 18:29
tompollard added a commit that referenced this pull request Nov 23, 2026
## Summary

Bump the version from 5.0.0 to 5.1.0, and collect the changelog entries
in `changelog.d/` into `docs/changes.rst` with `towncrier build --date
"Nov 2026"`, with a short summary at the top of the new section.

This is a minor release: it adds `physionet://` URIs and reads `pn_dir`
records and downloads databases through the `physionet` package, which
becomes a dependency (#657, #658, #659), speeds up `xqrs_detect` and
`gqrs_detect` (#655, #656), and fixes `rdann` and
`Annotation.apply_range` (#654, #660, #661). The one change existing
code may notice is that `rdann` now raises a `ValueError` when `sampto`
is not greater than `sampfrom`, including `sampto=0`, which used to
return every annotation. The summary calls it out.

It also raises the `physionet` requirement from 0.7.1 to 0.7.2, which
retries transient server errors when looking up a database's latest
version, as `pn_dir` without a version does, and updates that changelog
entry to match.

## Checks

- `uv run pytest -m "not network"`: 511 passed, also with `--python 3.11
--resolution lowest-direct` (physionet 0.7.2). `uv run pytest -m
network`: 18 passed.
- `ruff format --check`, `ruff check` and the Sphinx HTML build with
warnings as errors pass.
- The wheel and source distribution build, pass `twine check`, and have
`Version: 5.1.0` and `Requires-Dist: physionet>=0.7.2`.
- PyPI has no 5.1.0 release yet.

<sub>@tompollard 🟥 red 🦥 sloth</sub>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant